package com.example.database;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

@SuppressLint("NewApi") public class DatabaseHandler extends SQLiteOpenHelper {

	public DatabaseHandler(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	// Database Version
	private static final int DATABASE_VERSION = 1;

	// Database Name
	private static final String DATABASE_NAME = "WeightStorage";

	// Contacts table name
	private static final String TABLE_WEIGHT_DATE = "WeightDateTable";

	// Contacts Table Columns names
	private static final String KEY_WEIGHT = "weight";
	private static final String KEY_DATE = "date";


	public DatabaseHandler(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	public DatabaseHandler(Context context, String name, CursorFactory factory,
			int version, DatabaseErrorHandler errorHandler) {
		super(context, name, factory, version, errorHandler);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase arg0) {
		String CREATE_WEIGHT_DATE_TABLE = "CREATE TABLE " + TABLE_WEIGHT_DATE + "("
				+ KEY_WEIGHT + " INTEGER, " + KEY_DATE + " TEXT PRIMARY KEY" + ")";
		arg0.execSQL(CREATE_WEIGHT_DATE_TABLE);

	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		 Log.w(DatabaseHandler.class.getName(),
			        "Upgrading database from version " + arg1 + " to "
			            + arg2 + ", which will destroy all old data");
		arg0.execSQL("DROP TABLE IF EXISTS " + TABLE_WEIGHT_DATE);

		// Create tables again
		onCreate(arg0);
	}


	// Adding new contact
	public void addWeight(WeightStorage weightStorage) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_WEIGHT, weightStorage.getWeight()); 
		values.put(KEY_DATE, weightStorage.getDate());

		// Inserting Row
		db.insert(TABLE_WEIGHT_DATE, null, values);
		db.close(); // Closing database connection
	}

	public List<WeightStorage> getAllContacts() {
		List<WeightStorage> weightDateList = new ArrayList<WeightStorage>();
		// Select All Query
		String selectQuery = "SELECT  * FROM " + TABLE_WEIGHT_DATE;

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				WeightStorage weightDate = new WeightStorage();
				weightDate.setWeight(Integer.parseInt((cursor.getString(0))));
				weightDate.setDate(cursor.getString(1));
				// Adding contact to list
				weightDateList.add(weightDate);
			} while (cursor.moveToNext());
		}

		// return contact list
		return weightDateList;
	}

	public int getWeightDateCount() {
		String countQuery = "SELECT  * FROM " + TABLE_WEIGHT_DATE;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(countQuery, null);
		cursor.close();

		// return count
		return cursor.getCount();
	}

}
